//go:build ignore

package main

import (
	"fmt"
	"os"
	"path/filepath"
	"sort"
)

// middleware list order very important, handlers call via this order.
var middlewareList = []string{
	"recovery",
	"loop",
	"metrics",
	"accesslist",
	"ratelimit",
	"edns",
	"accesslog",
	"chaos",
	"hostsfile",
	"blocklist",
	"as112",
	"cache",
	"failover",
	"resolver",
	"forwarder",
}

func main() {
	var pathlist []string
	for _, name := range middlewareList {
		stat, err := os.Stat(filepath.Join(middlewareDir, name))
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
		if !stat.IsDir() {
			fmt.Println("path is not directory")
			os.Exit(1)
		}
		pathlist = append(pathlist, filepath.Join(prefixDir, middlewareDir, name))
	}

	file, err := os.Create(filename)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	defer file.Close()

	file.WriteString("// Code generated by gen.go DO NOT EDIT.\n")

	file.WriteString("\npackage main\n\nimport (\n")
	file.WriteString("\t\"github.com/semihalev/sdns/config\"\n")
	file.WriteString("\t\"github.com/semihalev/sdns/middleware\"\n")

	sort.StringSlice(pathlist).Sort()

	for _, path := range pathlist {
		file.WriteString("\t\"" + path + "\"\n")
	}

	file.WriteString(")\n\n")

	file.WriteString("func init() {\n")
	for _, name := range middlewareList {
		file.WriteString("\tmiddleware.Register(\"" + name + "\", func(cfg *config.Config) middleware.Handler { return " + name + ".New(cfg) })\n")
	}

	file.WriteString("}\n")
}

const (
	filename      = "zregister.go"
	prefixDir     = "github.com/semihalev/sdns"
	middlewareDir = "middleware"
)
